﻿using Common.Utility;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Web;

namespace Web.Filter
{
    public class ExceptionAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            try
            {
                LogHelper.Error(filterContext.Exception);
                //自定义异常的处理
                var result = new Result() { Code = -1 };
                if (filterContext.Exception is NotImplementedException)
                {
                    result.Msg = "系统异常，请稍后再进行尝试！";
                }
                else if (filterContext.Exception is TimeoutException)
                {
                    result.Msg = "请求超时！";
                }
                else
                {
                    string message = "发生错误";
                    result.Msg = message;
                }

                filterContext.Result = new JsonResult(result, new JsonSerializerSettings()
                {
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                });
            }
            catch (Exception e)
            {
                LogHelper.Error(e);
                throw new Exception(e.Message);
            }

        }
    }
}